package com.lgs.tab.customview;


import android.content.Context;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lgs.tab.R;

/**
 * Created by lgs on 2018/4/21.
 */

public class MyTabLayout extends TabLayout implements View.OnClickListener {

    private String[] mTitles = {"分时", "日K", "周K", "月K"};

    private MyPopupWindow myPopupWindow;
    Button mButton;

    public MyTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMyTabLayout(context);
    }

    private void initMyTabLayout(Context context) {
        for (int i = 0; i < 4; i++) {
            Tab tab = newTab();
            tab.setCustomView(getTabView(i));
            addTab(tab);
        }
        setOnTabSelectedListener(tabSelectedListener);
        // PopupWindow
        myPopupWindow = new MyPopupWindow(context);
//        myPopupWindow.setOnPopupWindowItemClickListener(this);
    }

    private View getTabView(int position) {
        View v = null;
        switch (position) {
            case 0:
            case 1:
            case 2:
            case 3:
                v = LayoutInflater.from(getContext().getApplicationContext()).inflate(R.layout.item_text_tab, null);
                TextView tv = (TextView) v.findViewById(R.id.textView);
                tv.setText(mTitles[position]);
                break;
            case 4:
                v = LayoutInflater.from(getContext().getApplicationContext()).inflate(R.layout.item_button_tab, null);
                mButton = (Button) v.findViewById(R.id.button);
                mButton.setOnClickListener(this);
        }
        return v;
    }

    private TabLayout.OnTabSelectedListener tabSelectedListener = new OnTabSelectedListener() {
        @Override
        public void onTabSelected(Tab tab) {
            // TODO 请求数据
            switch (tab.getPosition()) {
                case 0:
                    Toast.makeText(getContext().getApplicationContext(), "TODO 请求【分时】数据", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getContext().getApplicationContext(), "TODO 请求【日K】数据", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getContext().getApplicationContext(), "TODO 请求【周K】数据", Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getContext().getApplicationContext(), "TODO 请求【月K】数据", Toast.LENGTH_SHORT).show();
                    break;
                case 4:
//                    myPopupWindow.showPopupWindow(mButton);
                    break;
            }
        }

        @Override
        public void onTabUnselected(Tab tab) {

        }

        @Override
        public void onTabReselected(Tab tab) {

        }
    };


    @Override
    public void onClick(View view) {

    }
}
